﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.Xaml;
using System.Reflection;
using System.Diagnostics;
using NeuroSpeech.XamlExtensions;
using System.ComponentModel.Composition;
using NeuroSpeech.XamlGenerator.Code.Writers.Converters;

namespace NeuroSpeech.XamlExtensions
{
    public class DefaultConverter : XamlTypeGenerator
    {

        protected DefaultConverter()
        {

        }


        private static DefaultConverter[] converters = ConverterStore.Default.Converters;
        //new DefaultConverter[] { 
        //    new BindingConverter(),
        //    new AttachedPropertyConverter(),
        //    new EventConverter(),
        //    new DynamicResourceConverter(),
        //    new StaticResourceConverter(),
        //    new GridLengthConverter(),
        //    new ReferenceConverter(),
        //    new ThicknessConverter(),
        //    new RefConverter(),
        //    new ThisConverter(),
        //    new BrushConverter(),
        //    new ColorConverter()
        //};

        private static DefaultConverter Converter = new DefaultConverter { };

        public static XamlTypeGenerator GetConverter(
            XamlCodeContext context) 
        {

            XamlItem peek = context.Peek;

            if (peek.Type != null && peek.Type.UnderlyingType != null) {
                XamlGeneratorAttribute attr = GetGeneratorAttribute(peek.Type.UnderlyingType,context);
                if (attr != null)
                {
                    XamlTypeGenerator gen = GetConverter(context, attr);
                    if (gen != null)
                    {
                        if(gen.CanProcess(context))
                            return gen;
                    }
                }
            }

            DefaultConverter def = converters.FirstOrDefault(x => x.CanProcess(context));
            if (def != null)
                return def;
            return Converter;
        }

        private static XamlTypeGenerator GetConverter(XamlCodeContext context, XamlGeneratorAttribute attr)
        {
            if (attr.Type != null) {
                return Activator.CreateInstance(attr.Type) as XamlTypeGenerator;
            }
            Type type = LoadType(context.SchemaContext,attr.TypeName);
            if (type == null)
                return null;
            return Activator.CreateInstance(type) as XamlTypeGenerator;
        }

        private static Type LoadType(XamlSchemaContext schemaContext, string typeName)
        {
            foreach (Assembly asm in schemaContext.ReferenceAssemblies) {
                Type type = asm.GetType(typeName, false);
                if (type != null)
                    return type;
            }
            return null;
        }

        private static XamlGeneratorAttribute GetGeneratorAttribute(Type type, XamlCodeContext context)
        {
            foreach (dynamic att in type.GetCustomAttributes(true)) {
                if (att.GetType().FullName == "NeuroSpeech.XamlExtensions.XamlGeneratorAttribute") {
                    return new XamlGeneratorAttribute { 
                        TypeName = att.Type==null ? att.TypeName : att.Type.FullName
                    };
                }
            }
            return null;
        }

        


        public static void SetValue(XamlCodeContext context) {
            XamlItem peek = context.Peek;
            XamlMember member = peek.Member;
            XamlTypeGenerator def = GetConverter(context);
            ProcessValue(context, peek, def);
        }



        private static void ProcessValue(XamlCodeContext context, XamlItem peek, XamlTypeGenerator def)
        {
            Type targetType = context.Peek.MemberType;
            CodeExpression valueExpression = def.Transform(targetType, context.Peek.Value.Value);
            if (def.RequiresCasting(context))
            {
                valueExpression = def.TransformToType(targetType, valueExpression);
            }
            CodeStatement statement = def.Assign(context,peek.TargetObject, valueExpression);
            if (statement != null)
                context.Statements.Add(statement);
        }

        



        internal static void ExecuteStartObject(XamlCodeContext context)
        {
            XamlTypeGenerator converter = GetConverter(context);
            converter.StartObject(context);
        }
    }
}
